package com.app.tablet.view;

import java.sql.SQLException;
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.app.tablet.R;
import com.app.tablet.data.TabletData;
import com.app.tablet.data.dao.TabletDao;
import com.app.tablet.data.db.TabletDB;
import com.app.tablet.receiver.AlarmReceiver;

public class AddPageActivity extends BaseActivity implements OnClickListener {

	private static final String TagProject = "Tablet-Project";
	private static final String TagPage = "AddPageActivity-Page ::";

	TextView titletxt;
	Button leftBtn;
	Button rightBtn;
	static Button deleteBtn; 
	TextView txtDosage;
	TextView txtDosageSel;
	TextView txtSchedule;
	TextView txtScheduleSel;
	TextView txtMedname;
	TextView txtMednameSel;
	TextView txtAlarm;
	TextView txtAlarmSel;
	private RelativeLayout medicinenamerl;
	private RelativeLayout dosagerl;
	private RelativeLayout schedulerl;
	private RelativeLayout alarmsoundrl;

	TabletDao tabDAO = new TabletDao();
	public static TabletData dbData = new TabletData();

	@Override
	protected void onResume() {
		super.onResume();
		fillDosage();
		fillSchedule();
		fillMedicineName();
		fillAlarmTime();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_addpage);
		setHeaderContent();

		fillDosage();
		fillSchedule();
		fillMedicineName();
		fillAlarmTime();

		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
		medicinenamerl = (RelativeLayout) findViewById(R.id.medicinenamerl);
		medicinenamerl.setOnClickListener(this);
		dosagerl = (RelativeLayout) findViewById(R.id.dosagerl);
		dosagerl.setOnClickListener(this);
		schedulerl = (RelativeLayout) findViewById(R.id.schedulerl);
		schedulerl.setOnClickListener(this);
		alarmsoundrl = (RelativeLayout) findViewById(R.id.alarmsoundrl);
		alarmsoundrl.setOnClickListener(this);
	}

	private void setHeaderContent() {
		leftBtn = (Button) findViewById(R.id.leftHeaderBtn);
		leftBtn.setText(R.string.cancelBtn);

		deleteBtn = (Button)findViewById(R.id.deleteBtn);
		titletxt = (TextView) findViewById(R.id.titleHeaderTxt);
		rightBtn = (Button) findViewById(R.id.rightHeaderBtn);
		if (EditPageActivity.deleteBtnBool){
			rightBtn.setText(R.string.updateBtn);
			deleteBtn.setOnClickListener(this);
			deleteBtn.setVisibility(View.VISIBLE);
			titletxt.setText(R.string.titleEditpageTxt);
		}else{
			rightBtn.setText(R.string.doneBtn);
			deleteBtn.setVisibility(View.INVISIBLE);
			titletxt.setText(R.string.titletAddpageTxt);
		}
	}

	private void fillDosage() {
		txtDosage = (TextView) findViewById(R.id.txt_dosage);
		txtDosageSel = (TextView) findViewById(R.id.txt_dosage_selected);
		if (dbData.getDoasge() != null) {
			txtDosageSel.setText(dbData.getDoasge());
			txtDosageSel.setVisibility(View.VISIBLE);
			txtDosage.setTextColor(Color.BLACK);
		} else {
			txtDosageSel.setText(R.string.txt_dosage_selected);
			txtDosageSel.setVisibility(View.GONE);
			txtDosage.setTextColor(R.color.Sc5Txt3TimeR2_4Blue);
		}
	}

	private void fillSchedule() {
		txtSchedule = (TextView) findViewById(R.id.txt_schedule);
		txtScheduleSel = (TextView) findViewById(R.id.txt_schedule_selected);
		if (dbData.getSchedule() != null) {
			txtScheduleSel.setText(dbData.getSchedule());
			txtScheduleSel.setVisibility(View.VISIBLE);
			txtSchedule.setTextColor(Color.BLACK);
		} else {
			txtScheduleSel.setText(R.string.txt_schedule_selected);
			txtScheduleSel.setVisibility(View.GONE);
			txtSchedule.setTextColor(R.color.Sc5Txt3TimeR2_4Blue);
		}
	}

	private void fillMedicineName() {
		txtMedname = (TextView) findViewById(R.id.txt_medicationname);
		txtMednameSel = (TextView) findViewById(R.id.txt_medicationname_selected);
		if (dbData.getTabletname() != null) {
			txtMednameSel.setText(dbData.getTabletname());
			txtMednameSel.setVisibility(View.VISIBLE);
			txtMedname.setVisibility(View.INVISIBLE);
		} else {
			txtMednameSel.setText(R.string.txt_schedule_selected);
			txtMednameSel.setVisibility(View.GONE);
			txtMedname.setVisibility(View.VISIBLE);
			txtMedname.setTextColor(R.color.Sc5Txt3TimeR2_4Blue);
		}
	}

	private void fillAlarmTime() {
		txtAlarm = (TextView) findViewById(R.id.txt_alarmstart);
		txtAlarmSel = (TextView) findViewById(R.id.txt_time);
		if (dbData.getAlarmtime() != null) {
			txtAlarmSel.setText(dbData.getAlarmtime());
			txtAlarmSel.setVisibility(View.VISIBLE);
			txtAlarm.setTextColor(Color.BLACK);
		} else {
			txtAlarmSel.setText(R.string.txt_schedule_selected);
			txtAlarmSel.setVisibility(View.GONE);
			txtAlarm.setTextColor(R.color.Sc5Txt3TimeR2_4Blue);
		}
	}

	public static void reset() {
		dbData.setDoasge(null);
		dbData.setSchedule(null);
		dbData.setTabletname(null);
		dbData.setAlarmtime(null);
		dbData.setTones(null);
		if (EditPageActivity.deleteBtnBool){
			deleteBtn.setVisibility(View.INVISIBLE);
			EditPageActivity.deleteBtnBool=false;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftHeaderBtn:
//			if (EditPageActivity.deleteBtnBool){
//				EditPageActivity.deleteBtnBool=false;
//				deleteBtn.setVisibility(View.INVISIBLE);
//			}
			reset();
			this.finish();
			break;
		case R.id.rightHeaderBtn:
			Log.i(TagProject, TagPage+ dbData.getDoasge() +", "+dbData.getSchedule() +", "+ dbData.getTabletname()+", "+
					dbData.getAlarmtime()+", "+dbData.getTones());
			if (dbData.getDoasge() != null & dbData.getSchedule() != null
					& dbData.getTabletname() != null & dbData.getAlarmtime() != null & dbData.getTones() != null) {
				try {
					if (EditPageActivity.deleteBtnBool){
						Log.i(TagProject, TagPage+"updating...");
						tabDAO.update(TabletDB.getInstance());
						Log.i(TagProject, TagPage+"updated...");
						Toast.makeText(getApplicationContext(), "Reminder updated", 500).show();
						reset();
						this.finish();
						break;
					}else{
						tabDAO.insertnew(TabletDB.getInstance());
						Log.i(TagProject, TagPage+"START");
						startReminder(this);
//						shownotification();
						Toast.makeText(getApplicationContext(), "Reminder added", 500).show();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				reset();
				fillDosage();
				fillSchedule();
				fillMedicineName();
				fillAlarmTime();
				this.finish();
			}else{
				Toast.makeText(getApplicationContext(), "Choose all the five fields", 500).show();
			}
			break;
		case R.id.deleteBtn:
			Log.i(TagProject, TagPage+ dbData.getDoasge() +", "+dbData.getSchedule() +", "+ dbData.getTabletname()+", "+
					dbData.getAlarmtime()+", "+dbData.getTones());
			if (dbData.getDoasge() != null & dbData.getSchedule() != null
			& dbData.getTabletname() != null & dbData.getAlarmtime() != null & dbData.getTones() != null){
				try {
					tabDAO.delete(TabletDB.getInstance());
				} catch (SQLException e) {
					e.printStackTrace();
				}
				Toast.makeText(getApplicationContext(), "Reminder added", 500).show();
				reset();
				fillDosage();
				fillSchedule();
				fillMedicineName();
				fillAlarmTime();
//				EditPageActivity.deleteBtnBool=false;
//				deleteBtn.setVisibility(View.INVISIBLE);
				this.finish();
			}else{
				Toast.makeText(getApplicationContext(), "Choose all the five fields", 500).show();
			}
			break;
		case R.id.medicinenamerl:
			Intent in = new Intent(AddPageActivity.this,
					MedNameListActivity.class);
			startActivity(in);
			break;
		case R.id.dosagerl:
			Intent in2 = new Intent(AddPageActivity.this,
					DosageListActivity.class);
			startActivity(in2);
			break;
		case R.id.schedulerl:
			Intent in3 = new Intent(AddPageActivity.this,
					ScheduleListActivity.class);
			startActivity(in3);
			break;
		case R.id.alarmsoundrl:
			Intent in4 = new Intent(AddPageActivity.this,
					AlarmSetActivity.class);
			startActivity(in4);
			break;
		default:
			break;
		}
	}
	
	public void shownotification(){
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mymanager = (NotificationManager)getSystemService(ns);
		
		int icon = R.drawable.pills0;
		String timerText = "Tablet alarm saved.";
		long when = java.lang.System.currentTimeMillis();
		Notification notification = new Notification(icon, timerText, when);
		
		Context context = getApplicationContext();
		CharSequence contentTitle = "My TabletApp";
		CharSequence contentText = "Tblet Alarm!";
		Intent notificationIntent = new Intent(AddPageActivity.this, MainPageActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(AddPageActivity.this, 0, notificationIntent, 0);
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		
		final int HELLO_ID = 1;
		mymanager.notify(HELLO_ID, notification);
	}
	
	public void startReminder(Context context){
		Log.i(TagProject, TagPage+"stratReminder started.");
		
		Intent intentb = new Intent(context, AlarmReceiver.class);
		PendingIntent intentsender = PendingIntent.getBroadcast(context, 0, intentb, PendingIntent.FLAG_UPDATE_CURRENT);
		
		Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, dbData.getAlarmtimeINsec());
        
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), intentsender);
        
        Log.i(TagProject, TagPage+"startReminder finished.");
	}
	
	public void stopReminder(Context context){
		Log.i(TagProject, TagPage+"stopReminder started.");

		Intent intents = new Intent(context, AlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intents, PendingIntent.FLAG_CANCEL_CURRENT);
        
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
        
        Log.i(TagProject, TagPage+"stopReminder finished.");
	}
}